home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / sql-bench / bench-init.pl < prev    next >
Encoding:
Perl Script  |  2004-10-28  |  15.1 KB  |  599 lines

  1. #!/usr/bin/perl
  2. # Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. # Library General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Library General Public
  15. # License along with this library; if not, write to the Free
  16. # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  17. # MA 02111-1307, USA
  18. #
  19. ##########################################################
  20. # this is the base file every test is using ....
  21. # this is made for not changing every file if we want to
  22. # add an option or just want to change something in
  23. # code what is the same in every file ...
  24. ##########################################################
  25.  
  26. #
  27. # The exported values are:
  28.  
  29. # $opt_...    Various options
  30. # $date        Current date in ISO format
  31. # $server    Object for current server
  32. # $limits    Hash reference to limits for benchmark
  33.  
  34. $benchmark_version="2.15";
  35. use Getopt::Long;
  36. use POSIX;
  37.  
  38. require "$pwd/server-cfg" || die "Can't read Configuration file: $!\n";
  39.  
  40. $|=1;                # Output data immediately
  41.  
  42. $opt_skip_test=$opt_skip_create=$opt_skip_delete=$opt_verbose=$opt_fast_insert=$opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=$opt_log=$opt_use_old_results=$opt_help=$opt_odbc=$opt_small_test=$opt_small_tables=$opt_samll_key_tables=$opt_stage=$opt_old_headers=$opt_die_on_errors=$opt_tcpip=$opt_random=0;
  43. $opt_cmp=$opt_user=$opt_password=$opt_connect_options="";
  44. $opt_server="mysql"; $opt_dir="output";
  45. $opt_host="localhost";$opt_database="test";
  46. $opt_machine=""; $opt_suffix="";
  47. $opt_create_options=undef;
  48. $opt_optimization="None";
  49. $opt_hw="";
  50. $opt_threads=5;
  51.  
  52. if (!defined($opt_time_limit))
  53. {
  54.   $opt_time_limit=10*60;    # Don't wait more than 10 min for some tests
  55. }
  56.  
  57. $log_prog_args=join(" ", skip_arguments(\@ARGV,"comments","cmp","server",
  58.                     "user", "host", "database", "password",
  59.                     "use-old-results","skip-test",
  60.                     "optimization","hw",
  61.                     "machine", "dir", "suffix", "log"));
  62. GetOptions("skip-test=s","comments=s","cmp=s","server=s","user=s","host=s","database=s","password=s","loop-count=i","row-count=i","skip-create","skip-delete","verbose","fast-insert","lock-tables","debug","fast","force","field-count=i","regions=i","groups=i","time-limit=i","log","use-old-results","machine=s","dir=s","suffix=s","help","odbc","small-test","small-tables","small-key-tables","stage=i","threads=i","random","old-headers","die-on-errors","create-options=s","hires","tcpip","silent","optimization=s","hw=s","socket=s","connect-options=s") || usage();
  63.  
  64. usage() if ($opt_help);
  65. $server=get_server($opt_server,$opt_host,$opt_database,$opt_odbc,
  66.                    machine_part(), $opt_socket, $opt_connect_options);
  67. $limits=merge_limits($server,$opt_cmp);
  68. $date=date();
  69. @estimated=(0.0,0.0,0.0);        # For estimated time support
  70.  
  71. if ($opt_hires)
  72. {
  73.   eval "use Time::HiRes;";
  74. }
  75.  
  76. {
  77.   my $tmp= $opt_server;
  78.   $tmp =~ s/_odbc$//;
  79.   if (length($opt_cmp) && index($opt_cmp,$tmp) < 0)
  80.   {
  81.     $opt_cmp.=",$tmp";
  82.   }
  83. }
  84. $opt_cmp=lc(join(",",sort(split(',',$opt_cmp))));
  85.  
  86. #
  87. # set opt_lock_tables if one uses --fast and drivers supports it
  88. #
  89.  
  90. if (($opt_lock_tables || $opt_fast) && $server->{'limits'}->{'lock_tables'})
  91. {
  92.   $opt_lock_tables=1;
  93. }
  94. else
  95. {
  96.   $opt_lock_tables=0;
  97. }
  98. if ($opt_fast)
  99. {
  100.   $opt_fast_insert=1;
  101.   $opt_suffix="_fast" if (!length($opt_suffix));
  102. }
  103.  
  104. if ($opt_odbc)
  105. {
  106.    $opt_suffix="_odbc" if (!length($opt_suffix));
  107. }
  108.  
  109. if (!$opt_silent)
  110. {
  111.   print "Testing server '" . $server->version() . "' at $date\n\n";
  112. }
  113.  
  114. if ($opt_debug)
  115. {
  116.   print "\nCurrent limits: \n";
  117.   foreach $key (sort keys %$limits)
  118.   {
  119.     print $key . " " x (30-length($key)) . $limits->{$key} . "\n";
  120.   }
  121.   print "\n";
  122. }
  123.  
  124. #
  125. # Some help functions
  126. #
  127.  
  128. sub skip_arguments
  129. {
  130.   my($argv,@skip_args)=@_;
  131.   my($skip,$arg,$name,@res);
  132.  
  133.   foreach $arg (@$argv)
  134.   {
  135.     if ($arg =~ /^\-+([^=]*)/)
  136.     {
  137.       $name=$1;
  138.       foreach $skip (@skip_args)
  139.       {
  140.     if (index($skip,$name) == 0)
  141.     {
  142.       $name="";        # Don't use this parameters
  143.       last;
  144.     }
  145.       }
  146.       push (@res,$arg) if (length($name));
  147.     }
  148.   }
  149.   return @res;
  150. }
  151.  
  152.  
  153. sub merge_limits
  154. {
  155.   my ($server,$cmp)= @_;
  156.   my ($name,$tmp_server,$limits,$res_limits,$limit,$tmp_limits);
  157.  
  158.   $res_limits=$server->{'limits'};
  159.   if ($cmp)
  160.   {
  161.     foreach $name (split(",",$cmp))
  162.     {
  163.       $tmp_server= (get_server($name,$opt_host, $opt_database,
  164.                    $opt_odbc,machine_part())
  165.             || die "Unknown SQL server: $name\n");
  166.       $limits=$tmp_server->{'limits'};
  167.       %new_limits=();
  168.       foreach $limit (keys(%$limits))
  169.       {
  170.     if (defined($res_limits->{$limit}) && defined($limits->{$limit}))
  171.     {
  172.       $new_limits{$limit}=min($res_limits->{$limit},$limits->{$limit});
  173.     }
  174.       }
  175.       %tmp_limits=%new_limits;
  176.       $res_limits=\%tmp_limits;
  177.     }
  178.   }
  179.   return $res_limits;
  180. }
  181.  
  182. sub date
  183. {
  184.   my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time());
  185.   sprintf("%04d-%02d-%02d %2d:%02d:%02d",
  186.       1900+$year,$mon+1,$mday,$hour,$min,$sec);
  187. }
  188.  
  189. sub min
  190. {
  191.   my($min)=$_[0];
  192.   my($i);
  193.   for ($i=1 ; $i <= $#_; $i++)
  194.   {
  195.     $min=$_[$i] if ($min > $_[$i]);
  196.   }
  197.   return $min;
  198. }
  199.  
  200. sub max
  201. {
  202.   my($max)=$_[0];
  203.   my($i);
  204.   for ($i=1 ; $i <= $#_; $i++)
  205.   {
  206.     $max=$_[$i] if ($max < $_[$i]);
  207.   }
  208.   return $max;
  209. }
  210.  
  211.  
  212. #
  213. # Execute many statements in a row
  214. #
  215.  
  216. sub do_many
  217. {
  218.   my ($dbh,@statements)=@_;
  219.   my ($statement,$sth);
  220.  
  221.   foreach $statement (@statements)
  222.   {
  223.     if (!($sth=$dbh->do($statement)))
  224.     {
  225.       die "Can't execute command '$statement'\nError: $DBI::errstr\n";
  226.     }
  227.   }
  228. }
  229.  
  230. sub safe_do_many
  231. {
  232.   my ($dbh,@statements)=@_;
  233.   my ($statement,$sth);
  234.  
  235.   foreach $statement (@statements)
  236.   {
  237.     if (!($sth=$dbh->do($statement)))
  238.     {
  239.       print STDERR "Can't execute command '$statement'\nError: $DBI::errstr\n";
  240.       return 1;
  241.     }
  242.   }
  243.   return 0;
  244. }
  245.  
  246.  
  247.  
  248. #
  249. # Do a query and fetch all rows from a statement and return the number of rows
  250. #
  251.  
  252. sub fetch_all_rows
  253. {
  254.   my ($dbh,$query,$must_get_result)=@_;
  255.   my ($count,$sth);
  256.   $count=0;
  257.  
  258.   print "$query: " if ($opt_debug);
  259.   if (!($sth= $dbh->prepare($query)))
  260.   {
  261.     print "\n" if ($opt_debug);
  262.     die "Error occured with prepare($query)\n -> $DBI::errstr\n";
  263.     return undef;
  264.   }
  265.   if (!$sth->execute)
  266.   {
  267.     print "\n" if ($opt_debug);
  268.     if (defined($server->{'error_on_execute_means_zero_rows'}) &&
  269.        !$server->abort_if_fatal_error())
  270.     {
  271.       if (defined($must_get_result) && $must_get_result)
  272.       {
  273.     die "Error: Query $query didn't return any rows\n";
  274.       }
  275.       $sth->finish;
  276.       print "0\n" if ($opt_debug);
  277.       return 0;
  278.     }
  279.     die "Error occured with execute($query)\n -> $DBI::errstr\n";
  280.     $sth->finish;
  281.     return undef;
  282.   }
  283.   while ($sth->fetchrow_arrayref)
  284.   {
  285.     $count++;
  286.   }
  287.   print "$count\n" if ($opt_debug);
  288.   if (defined($must_get_result) && $must_get_result && !$count)
  289.   {
  290.     die "Error: Query $query didn't return any rows\n";
  291.   }
  292.   $sth->finish;
  293.   undef($sth);
  294.   return $count;
  295. }
  296.  
  297. sub do_query
  298. {
  299.   my($dbh,$query)=@_;
  300.   print "$query\n" if ($opt_debug);
  301.   $dbh->do($query) or
  302.     die "\nError executing '$query':\n$DBI::errstr\n";
  303. }
  304.  
  305. #
  306. # Run a query X times
  307. #
  308.  
  309. sub time_fetch_all_rows
  310. {
  311.   my($test_text,$result_text,$query,$dbh,$test_count)=@_;
  312.   my($i,$loop_time,$end_time,$count,$rows,$estimated);
  313.  
  314.   print $test_text . "\n"   if (defined($test_text));
  315.   $count=$rows=0;
  316.   $loop_time=new Benchmark;
  317.   for ($i=1 ; $i <= $test_count ; $i++)
  318.   {
  319.     $count++;
  320.     $rows+=fetch_all_rows($dbh,$query) or die $DBI::errstr;
  321.     $end_time=new Benchmark;
  322.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i,
  323.                        $test_count));
  324.   }
  325.   $end_time=new Benchmark;
  326.   if ($estimated)
  327.   { print "Estimated time"; }
  328.   else
  329.   { print "Time"; }
  330.   print " for $result_text ($count:$rows) " .
  331.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  332. }
  333.  
  334.  
  335. #
  336. # Handle estimated time of the server is too slow
  337. # Returns 0 if one should continue as normal
  338. #
  339.  
  340. sub predict_query_time
  341. {
  342.   my ($loop_time,$end_time,$count_ref,$loop,$loop_count)= @_;
  343.   my ($k,$tmp);
  344.  
  345.   if (($end_time->[0] - $loop_time->[0]) > $opt_time_limit)
  346.   {
  347.     # We can't wait until the SUN dies.  Try to predict the end time
  348.     if ($loop != $loop_count)
  349.     {
  350.       $tmp=($end_time->[0] - $loop_time->[0]);
  351.       print "Note: Query took longer then time-limit: $opt_time_limit\nEstimating end time based on:\n";
  352.       print "$$count_ref queries in $loop loops of $loop_count loops took $tmp seconds\n";
  353.       for ($k=0; $k < 3; $k++)
  354.       {
  355.     $tmp=$loop_time->[$k]+($end_time->[$k]-$loop_time->[$k])/$loop*
  356.       $loop_count;
  357.     $estimated[$k]+=($tmp-$end_time->[$k]);
  358.     $end_time->[$k]=$tmp;
  359.       }
  360.       $$count_ref= int($$count_ref/$loop*$loop_count);
  361.       return 1;
  362.     }
  363.   }
  364.   return 0;
  365. }
  366.  
  367. #
  368. # standard end of benchmark
  369. #
  370.  
  371. sub end_benchmark
  372. {
  373.   my ($start_time)=@_;
  374.  
  375.   $end_time=new Benchmark;
  376.   if ($estimated[0])
  377.   {
  378.     print "Estimated total time: ";
  379.     $end_time->[0]+=$estimated[0];
  380.     $end_time->[1]+=$estimated[1];
  381.     $end_time->[2]+=$estimated[2];
  382.   }
  383.   else
  384.   {
  385.     print "Total time: "
  386.     }
  387.   print timestr(timediff($end_time, $start_time),"all") . "\n";
  388.   exit 0;
  389. }
  390.  
  391. sub print_time
  392. {
  393.   my ($estimated)=@_;
  394.   if ($estimated)
  395.   { print "Estimated time"; }
  396.   else
  397.   { print "Time"; }
  398. }
  399.  
  400. #
  401. # Create a filename part for the machine that can be used for log file.
  402. #
  403.  
  404. sub machine_part
  405. {
  406.   my ($name,$orig);
  407.   return $opt_machine if (length($opt_machine)); # Specified by user
  408. # Specified by user
  409.   $orig=$name=machine();
  410.   $name="win9$1" if ($orig =~ /win.*9(\d)/i);
  411.   $name="NT_$1" if ($orig =~ /Windows NT.*(\d+\.\d+)/i);
  412.   $name="win2k" if ($orig =~ /Windows 2000/i);
  413.   $name =~ s/\s+/_/g;        # Make the filenames easier to parse
  414.   $name =~ s/-/_/g;
  415.   $name =~ s/\//_/g;
  416.   return $name;
  417. }
  418.  
  419. sub machine
  420. {
  421.   my @name = POSIX::uname();
  422.   my $name= $name[0] . " " . $name[2] . " " . $name[4];
  423.   return $name;
  424. }
  425.  
  426. #
  427. # Usage
  428. #
  429.  
  430. sub usage
  431. {
  432.     print <<EOF;
  433. The MySQL benchmarks Ver $benchmark_version
  434.  
  435. All benchmarks takes the following options:
  436.  
  437. --comments
  438.   Add a comment to the benchmark output.  Comments should contain
  439.   extra information that 'uname -a' doesn\'t give and if the database was
  440.   stared with some specific, non default, options.
  441.  
  442. --cmp=server[,server...]
  443.   Run the test with limits from the given servers.  If you run all servers
  444.   with the same --cmp, you will get a test that is comparable between
  445.   the different sql servers.
  446.  
  447. --create-options=#
  448.   Extra argument to all create statements.  If you for example want to
  449.   create all MySQL tables as BDB tables use:
  450.   --create-options=TYPE=BDB
  451.  
  452. --database (Default $opt_database)
  453.   In which database the test tables are created.
  454.  
  455. --debug
  456.   This is a test specific option that is only used when debugging a test.
  457.   Print out debugging information.
  458.  
  459. --dir (Default $opt_dir)
  460.   Option to 'run-all-tests' to where the test results should be stored.
  461.  
  462. --fast
  463.   Allow the database to use non standard ANSI SQL commands to make the
  464.   test go faster.
  465.  
  466. --fast-insert
  467.   Use "insert into table_name values(...)" instead of
  468.   "insert into table_name (....) values(...)"
  469.   If the database supports it, some tests uses multiple value lists.
  470.  
  471. --field-count
  472.   This is a test specific option that is only used when debugging a test.
  473.   This usually means how many fields there should be in the test table.
  474.  
  475. --force
  476.   This is a test specific option that is only used when debugging a test.
  477.   Continue the test even if there is some error.
  478.   Delete tables before creating new ones.
  479.  
  480. --groups (Default $opt_groups)
  481.   This is a test specific option that is only used when debugging a test.
  482.   This usually means how many different groups there should be in the test.
  483.  
  484. --lock-tables
  485.   Allow the database to use table locking to get more speed.
  486.  
  487. --log
  488.   Option to 'run-all-tests' to save the result to the '--dir' directory.
  489.  
  490. --loop-count (Default $opt_loop_count)
  491.   This is a test specific option that is only used when debugging a test.
  492.   This usually means how many times times each test loop is executed.
  493.  
  494. --help
  495.   Shows this help
  496.  
  497. --host='host name' (Default $opt_host)
  498.   Host name where the database server is located.
  499.  
  500. --machine="machine or os_name"
  501.   The machine/os name that is added to the benchmark output filename.
  502.   The default is the OS name + version.
  503.  
  504. --odbc
  505.   Use the ODBC DBI driver to connect to the database.
  506.  
  507. --password='password'
  508.   Password for the current user.
  509.  
  510. --socket='socket'
  511.   If the database supports connecting through a Unix socket,
  512.   then use this socket to connect
  513.  
  514. --regions
  515.   This is a test specific option that is only used when debugging a test.
  516.   This usually means how AND levels should be tested.
  517.  
  518. --old-headers
  519.   Get the old benchmark headers from the old RUN- file.
  520.  
  521. --server='server name'  (Default $opt_server)
  522.   Run the test on the given SQL server.
  523.   Known servers names are: Access, Adabas, AdabasD, Empress, Oracle,
  524.   Informix, DB2, mSQL, MS-SQL, MySQL, Pg, Solid and Sybase
  525.  
  526. --silent
  527.   Don't print info about the server when starting test.
  528.  
  529. --skip-delete
  530.   This is a test specific option that is only used when debugging a test.
  531.   This will keep the test tables after the test is run.
  532.  
  533. --skip-test=test1[,test2,...]
  534.   For run-all-programs;  Don\'t execute the named tests.
  535.  
  536. --small-test
  537.   This runs some tests with smaller limits to get a faster test.
  538.   Can be used if you just want to verify that the database works, but
  539.   don't have time to run a full test.
  540.  
  541. --small-tables
  542.   This runs some tests that generate big tables with fewer rows.
  543.   This can be used with databases that can\'t handle that many rows
  544.   because of pre-sized partitions.
  545.  
  546. --suffix (Default $opt_suffix)
  547.   The suffix that is added to the database name in the benchmark output
  548.   filename.  This can be used to run the benchmark multiple times with
  549.   different server options without overwritten old files.
  550.   When using --fast the suffix is automaticly set to '_fast'.
  551.  
  552. --random
  553.   Inform test suite that we are generate random inital values for sequence of
  554.   test executions. It should be used for imitation of real conditions.
  555.  
  556. --threads=# (Default 5)
  557.   Number of threads for multi-user benchmarks.
  558.  
  559. --tcpip
  560.   Inform test suite that we are using TCP/IP to connect to the server. In
  561.   this case we can\t do many new connections in a row as we in this case may
  562.   fill the TCP/IP stack
  563.  
  564. --time-limit (Default $opt_time_limit)
  565.   How long a test loop is allowed to take, in seconds, before the end result
  566.   is 'estimated'.
  567.  
  568. --use-old-results
  569.   Option to 'run-all-tests' to use the old results from the  '--dir' directory
  570.   instead of running the tests.
  571.  
  572. --user='user_name'
  573.   User name to log into the SQL server.
  574.  
  575. --verbose
  576.   This is a test specific option that is only used when debugging a test.
  577.   Print more information about what is going on.
  578.  
  579. --optimization='some comments'
  580.  Add coments about optimization of DBMS, which was done before the test.
  581.  
  582. --hw='some comments'
  583.  Add coments about hardware used for this test.
  584.  
  585. --connect-options='some connect options'
  586.   Add options, which uses at DBI connect.
  587.   For example --connect-options=mysql_read_default_file=/etc/my.cnf.
  588.  
  589. EOF
  590.   exit(0);
  591. }
  592.  
  593.  
  594.  
  595. ####
  596. #### The end of the base file ...
  597. ####
  598. 1;
  599.